package com.ebc.eservice.controller.pc;

import com.ebc.eservice.aop.MyLog;
import com.ebc.eservice.pojo.EbcUser;
import com.ebc.eservice.service.impl.login.LoginService;
import com.ebc.eservice.service.impl.login.LoginServiceImp;
import com.ebc.eservice.util.CurrentUser;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.util.common.constant.ApiTextHelperConstant;
import com.ebc.eservice.util.common.constant.DeveloperConstant;
import com.ebc.eservice.util.common.constant.LogConstant;
import com.ebc.eservice.vo.user.LoginUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * @author: Joey
 * @Description:后台管理系统-登录-PC登录管理
 * @date:2022/9/18 0:26
 */
@Api(tags = "后台管理系统-登录")
@RestController
@Slf4j
@MyLog  //自动捕获异常、正常日志输出需在方法上加注解
//@RequestMapping("/api")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "登录", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/user/login")
    public JsonResult LoginToken(@RequestBody LoginUserVo loginUserVo){
        log.info("当前登录用户信息:{}", loginUserVo);
        return loginService.login(loginUserVo);
    }

    @ApiOperation(value = "注销", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/user/logout")
    public JsonResult logout(){
        CurrentUser user = new CurrentUser();
        int currentId = user.getCurrentUser();
        System.out.println("=======PC端用户退出, id: "+currentId);
        return loginService.logout();
    }




    //测试生成token

    @GetMapping("/user/c")
    public JsonResult ToToken(){
        String n = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println("当前用户名"+n);
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        System.out.println("->已授予的权限"+authorities);
        System.out.println("===================权限1");
        return JsonResult.success("我是CCCCCCCCCC");
    }
}
